import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import UserList from '../components/userList.vue'
import UserRight from '../components/userRight.vue'
import Notice from '../components/notice.vue'
import Check from '../components/check.vue'
import Email from '../components/email.vue'
import Talk from '../components/talk.vue'
import Addr from '../components/addr.vue'
import Sal from '../components/sal.vue'


Vue.use(VueRouter)
const routes = [
  {path: '/',redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/elementUI', component: ElementUI},
  {path: '/home', component: Home, meta: {title: 'OA办公自动化系统'}, redirect: '/welcome', children: [
    {path: '/welcome', component: Welcome},
    {path: '/check', component: Check},
    {path: '/userList', component: UserList},
    {path: '/userRight', component: UserRight},
    {path: '/notice', component: Notice},
    {path: '/email', component: Email},
    {path: '/sal', component: Sal},
    {path: '/talk', component: Talk},
    {path: '/addr', component: Addr}
  ]}
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  if(to.meta.title) {
    document.title = to.meta.title
  }

  if(to.path === "/login") return next()

  let token = window.sessionStorage.getItem('token')
  if(token == null || token.length == 0) {
    return next("/login")
  } else {
    return next()
  }
})

export default router
